﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using COC.Graphics;
using COC.Util;
using XNAnimation;

namespace COC.GameLogic
{
	public class AnimActor : WActorBase, IDisposable
	{
		protected readonly string m_ModelName = "";
		public string ModelName
		{
			get { return m_ModelName; }
		}

		protected readonly SingleCharacter m_ActorCharacter;
		public SingleCharacter Character
		{
			get { return m_ActorCharacter; }
		}

		protected readonly SkinnedModel m_SkModel;
		public SkinnedModel Model
		{
			get { return m_SkModel; }
		}

		protected ICullable m_Culling = new ICullable();
		public BoundingBox BoundingBox
		{
			get { return m_Culling.BoundingBox; }
		}

		protected bool m_Diposed = false;

		public AnimActor(string actorName, string mdlName)
			: base(actorName)
		{
			m_ModelName = mdlName;
			m_ActorCharacter = CoCGame.Singleton.CurrentGameWorld.m_Animation.CreateInstance(
				mdlName, actorName, Vector3.Zero, Vector3.Zero, out m_SkModel);

			if (m_ActorCharacter == null || m_SkModel == null)
			{
				throw new Exception("ModelNotFound");
			}

			BoundingBox bb = m_Culling.BoundingBox;

			foreach (ModelMesh modelMesh in m_SkModel.Model.Meshes)
			{
				BoundingSphere bs = modelMesh.BoundingSphere;
				Vector3 max = bs.Center + new Vector3(bs.Radius);
				Vector3 min = bs.Center - new Vector3(bs.Radius);
				Intersection.EnlargeBoundingBox(ref bb, min, max);
			}

			m_Culling.BoundingBox = bb;
		}

		public void Dispose()
		{
			m_Diposed = true;
			CoCGame.Singleton.CurrentGameWorld.m_Animation.RemoveInstance(this);
		}
	}
}